//
//  CLGCollectionViewController.m
//  CLGReorderColumnDemo
//
//  Created by GuochengLiu on 2016/11/18.
//  Copyright © 2016年 sumavision. All rights reserved.
//

#import "CLGCollectionViewController.h"
#import "CLGCollectionViewCell.h"

#import "TSReorderColumnViewController.h"

@interface CLGCollectionViewController ()

@end

@implementation CLGCollectionViewController
{
    NSMutableArray *_arrayFirstSection;
    NSMutableArray *_arraySecondSection;
    NSArray *_arrayData;
}

static NSString * const reuseIdentifier = @"CLGCollectionViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CLGCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    
    _arrayFirstSection = [@[@"1", @"2", @"3", @"4", @"5", @"6"] mutableCopy];
    _arraySecondSection = [@[@"A", @"B", @"C", @"D", @"E", @"F"] mutableCopy];
    
    _arrayData = @[_arrayFirstSection, _arraySecondSection];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"低于iOS9" style:UIBarButtonItemStylePlain target:self action:@selector(onHitRightBarButton:)];
}

- (void)onHitRightBarButton:(UIBarButtonItem *)sender
{
    TSReorderColumnViewController *reorderVC = [[TSReorderColumnViewController alloc] initWithNibName:@"TSReorderColumnViewController" bundle:nil];
    
    [self.navigationController pushViewController:reorderVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _arrayData.count;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_arrayData[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CLGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor grayColor];
    
    NSString *name = _arrayData[indexPath.section][indexPath.row];
    [cell.nameLabel setText:name];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSIndexPath *destIndexPath;
    id item = _arrayData[indexPath.section][indexPath.row];
    
    
    NSLog(@"before: _arrayData %@", _arrayData);
    
    if (indexPath.section == 0) {
    
        NSArray *arrayDestSection = _arrayData[1];
        
        destIndexPath = [NSIndexPath indexPathForRow:arrayDestSection.count inSection:1];
        
        [collectionView performBatchUpdates:^{
            
            NSMutableArray *arraySource = _arrayData[indexPath.section];
            [arraySource removeObject:item];
            NSMutableArray *arrayDest = _arrayData[destIndexPath.section];
            [arrayDest addObject:item];
            
            [collectionView moveItemAtIndexPath:indexPath toIndexPath:destIndexPath];
        } completion:nil];
        
    } else {
        NSArray *arrayDestSection = _arrayData[0];
        
        destIndexPath = [NSIndexPath indexPathForRow:arrayDestSection.count inSection:0];
        
        [collectionView performBatchUpdates:^{

            
            NSMutableArray *arraySource = _arrayData[indexPath.section];
            [arraySource removeObject:item];
            
            NSMutableArray *arrayDest = _arrayData[destIndexPath.section];
            [arrayDest addObject:item];
            
            [collectionView moveItemAtIndexPath:indexPath toIndexPath:destIndexPath];
        } completion:nil];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"after _arrayData %@", _arrayData);
    });
    

}


- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    
    id item = _arrayData[sourceIndexPath.section][sourceIndexPath.row];
    
    NSInteger sourceSection = sourceIndexPath.section;
    NSMutableArray *arraySource = _arrayData[sourceSection];
    
    
    [arraySource removeObject:item];
    
    
    NSInteger destSection = destinationIndexPath.section;
    NSMutableArray *arrayDest = _arrayData[destSection];
    
    [arrayDest insertObject:item atIndex:destinationIndexPath.row];
}

@end
